package org.dromara.common.mybatis.core.domain;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Entity基类
 */

@Data
public class BaseEntity implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 搜索值
   */
  @JsonIgnore
  @TableField(exist = false)
  private String searchValue;

  /**
   * 创建部门
   */
  @TableField(fill = FieldFill.INSERT)
  private Long createDept;

  /**
   * 创建者
   */
  @TableField(fill = FieldFill.INSERT)
  private Long createBy;

  /**
   * 创建时间
   */
  @TableField(fill = FieldFill.INSERT)
  private Date createTime;

  /**
   * 更新者
   */
  @TableField(fill = FieldFill.INSERT_UPDATE)
  private Long updateBy;

  /**
   * 更新时间
   */
  @TableField(fill = FieldFill.INSERT_UPDATE)
  private Date updateTime;

  /**
   * 请求参数
   */
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @TableField(exist = false)
  private Map<String, Object> params = new HashMap<>();

  public Map<String, Object> getParams() {
    if (params == null) {
      params = new HashMap<>();
    }
    return params;
  }

  public void setParams(Map<String, Object> params) {
    this.params = params;
  }

  public void setParams(String str, Object obj) {
    if (this.params == null) {
      this.params = new HashMap<>();
    }
    this.params.put(str, obj);
  }

  public String getParamsToString(String str) {
    if (this.params == null) {
      return null;
    }
    return this.params.get(str).toString();
  }

  public Double getParamsToDouble(String str) {
    if (this.params == null) {
      return null;
    }
    return Double.parseDouble((String)this.params.get(str));
  }

  public Long getParamsToLong(String str) {
    if (this.params == null) {
      return null;
    }
    return (Long) this.params.get(str);
  }

  public Integer getParamsToInt(String str) {
    if (this.params == null) {
      return null;
    }
    return Integer.parseInt(this.getParamsToString(str)) ;
  }
}
